/**
 * @Copyright FangYun Home
 */
package home.fangyun.pagerank.domain.importance;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import home.fangyun.pagerank.domain.Assessable;

/**
 * @author fangyuncn@163.com
 */
public class ThingAssessment implements Assessable<Thing> {
	private List<Thing> things;

	public ThingAssessment(Path path) throws IOException {
		List<String> ls = Files.readAllLines(path);
		things = new ArrayList<>();
		for (String l : ls) {
			int dot = l.indexOf(".");
			String f = l.substring(0, dot);
			int j = l.indexOf("->");
			String description = l.substring(dot + 1);
			String ts = "-1";
			if (j >= 0) {
				description = l.substring(dot + 1, j);
				ts = l.substring(j + 2);
			}
			int from = Integer.parseInt(f);
			String[] tosStr = new String[0];
			if (!ts.equals("-1")) {
				tosStr = ts.split(",");
			}
			int[][] tos = new int[tosStr.length][2];
			for (int i = 0; i < tos.length; i++) {
				String[] tosArr = tosStr[i].trim().split("\\.");
				int to = Integer.parseInt(tosArr[0]);
				int rating = Integer.parseInt(tosArr[1]);
				tos[i][0] = to;
				tos[i][1] = rating;
			}
			Thing t = new Thing(from, tos, description);
			things.add(t);
		}

	}

	@Override
	public List<Thing> getVotes() {
		return things;
	}
}
